package com.zzgc.modules.service.impl;

import com.zzgc.common.dao.AuthRightDao;
import com.zzgc.common.dao.AuthRoleDao;
import com.zzgc.common.dao.AuthRoleRightDao;
import com.zzgc.common.entity.AuthRight;
import com.zzgc.common.entity.AuthRole;
import com.zzgc.modules.controller.BaseController;
import com.zzgc.modules.service.AuthRoleService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色表(AuthRole)表服务实现类
 *
 * @author makejava
 * @since 2022-11-18 09:05:45
 */
@Service("authRoleService")
public class AuthRoleServiceImpl implements AuthRoleService {
    @Resource
    private AuthRoleDao authRoleDao;
    @Resource
    private AuthRightDao authRightDao;
    @Resource
    private AuthRoleRightDao authRoleRightDao;
    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AuthRole queryById(Long id) {
        return this.authRoleDao.queryById(id);
    }

    /**
     *  根据权限ID获取权限列表
     * @Author
     * @Description
     * @Date  2022/12/20 11:00
     * @Param
     * @return
     */
    @Override
    public List<AuthRight> getRoleRights(Long roleId) {
        // 获取全菜单内容
        List<AuthRight> authRights = authRightDao.queryByParentId(0L);
        //authRights.forEach(System.out::println);
        // 获取角色的菜单权限
        List<AuthRight> authRoleRights = authRoleRightDao.queryByRoleId(roleId);
        // authRoleRights.forEach(System.out::println);

        // 记录一级剔除权限
        ArrayList<AuthRight> arrayList1 = new ArrayList<>();
        // 比较删除多余的
        authRights.forEach(authRight -> {
            // 比较二级菜单
            List<AuthRight> rights = authRight.getRights();
            // 记录二级菜单剔除
            ArrayList<AuthRight> arrayList2 = new ArrayList<>();
            rights.forEach(authRight1 -> {
                // 需要重写AuthRight的equals方法判断id是否相等
                if (!authRoleRights.contains(authRight1)){
                    arrayList2.add(authRight1);
                }
            });
            rights.removeAll(arrayList2);
            if (!authRoleRights.contains(authRight)) {
                arrayList1.add(authRight);
            }
        });
        authRights.removeAll(arrayList1);
        return authRights;
    }


    /**
     * 分页查询
     *
     * @param authRole 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<AuthRole> queryByPage(AuthRole authRole, PageRequest pageRequest) {
        long total = this.authRoleDao.count(authRole);
        return new PageImpl<>(this.authRoleDao.queryAllByLimit(authRole), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param authRole 实例对象
     * @return 实例对象
     */
    @Override
    public AuthRole insert(AuthRole authRole) {
        this.authRoleDao.insert(authRole);
        return authRole;
    }

    /**
     * 修改数据
     *
     * @param authRole 实例对象
     * @return 实例对象
     */
    @Override
    public AuthRole update(AuthRole authRole) {
        this.authRoleDao.update(authRole);
        return this.queryById(authRole.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.authRoleDao.deleteById(id) > 0;
    }
}
